package view;

import android.R.integer;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class _CustomViewUseScroller extends ViewGroup{

	private String TAG = _CustomViewUseScroller.class.getSimpleName();
	Scroller mScroller;
	int curScreen = 0;
	
	int TouchSlop = 0;
	
	public _CustomViewUseScroller(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		
		mScroller = new Scroller(context);
		init();
	}
	
	
	void init(){
		
		TouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
		
		/*  OK
		//创建3个子项
		LinearLayout layout_a = new LinearLayout(getContext());
		LinearLayout layout_b = new LinearLayout(getContext());
		LinearLayout layout_c = new LinearLayout(getContext());
		
		layout_a.setBackgroundColor(Color.RED);
		layout_b.setBackgroundColor(Color.GREEN);
		layout_c.setBackgroundColor(Color.BLUE);
		
		
		addView(layout_a);
		addView(layout_b);
		addView(layout_c);
		*/
	}
	

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		int childLeft = 0;
		final int childCount = getChildCount();
		System.out.println("childCount: "+childCount);
		for (int i = 0; i < childCount; i++) {
			final View childView = getChildAt(i);
			if (childView.getVisibility() != View.GONE) {
				final int childWidth = childView.getMeasuredWidth();
				childView.layout(childLeft, 0, childLeft + childWidth,
						childView.getMeasuredHeight());
				childLeft += childWidth;
			}
		}

		
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		//Log.e(TAG, "onMeasure");
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int width = MeasureSpec.getSize(widthMeasureSpec);
		final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		if (widthMode != MeasureSpec.EXACTLY) {
			throw new IllegalStateException(
					"ScrollLayout only canmCurScreen run at EXACTLY mode!");
		}
		final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		if (heightMode != MeasureSpec.EXACTLY) {
			throw new IllegalStateException(
					"ScrollLayout only can run at EXACTLY mode!");
		}

		// The children are given the same width and height as the scrollLayout
		final int count = getChildCount();
		System.out.println("onMeasure count: "+count);
		for (int i = 0; i < count; i++) {
			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
		}
		Log.e(TAG, "moving to screen "+curScreen);
		
		scrollTo(curScreen * width, 0);
	}
	
	
	 //startScroll开始移至下一屏  
    public void startMovePre(){  
       
        //使用动画控制偏移过程 , 3s内到位  
        mScroller.startScroll((curScreen) * getWidth(), 0, -getWidth(), 0,3000);   //得到以页面右边为偏移值
        //其实点击按钮的时候，系统会自动重新绘制View，我们还是手动加上吧。  
        invalidate();  
        //使用scrollTo一步到位  
        //scrollTo(curScreen * MultiScreenActivity.screenWidth, 0);  
        
        curScreen -- ;  
        Log.i(TAG, "----startMovePre---- curScreen " + curScreen);  
        
    }  
    
	
	 //startScroll开始移至下一屏  
    public void startMoveNext(){  
        curScreen ++ ;  
        Log.i(TAG, "----startMoveNext---- curScreen " + curScreen);  
          
        //使用动画控制偏移过程 , 3s内到位  
        mScroller.startScroll((curScreen-1) * getWidth(), 0, getWidth(), 0,3000);  
        //其实点击按钮的时候，系统会自动重新绘制View，我们还是手动加上吧。  
        invalidate();  
        //使用scrollTo一步到位  
        //scrollTo(curScreen * MultiScreenActivity.screenWidth, 0);  
    }  
    
	
	// 由父视图调用用来请求子视图根据偏移值 mScrollX,mScrollY重新绘制  
    @Override  
    public void computeScroll() {     
        // TODO Auto-generated method stub  
        Log.d(TAG, "computeScroll");  
        // 如果返回true，表示动画还没有结束  
        // 因为前面startScroll，所以只有在startScroll完成时 才会为false  
        if (mScroller.computeScrollOffset()) {  
            Log.d(TAG, mScroller.getCurrX() + "======" + mScroller.getCurrY());  
            // 产生了动画效果，根据当前值 每次滚动一点  
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
              
            Log.d(TAG, "### getleft is " + getLeft() + " ### getRight is " + getRight());  
            //此时同样也需要刷新View ，否则效果可能有误差  
            postInvalidate();  
        }  
        else  
            Log.i(TAG, "have done the scoller -----");  
    }  
    
    
  //马上停止移动，如果已经超过了下一屏的一半，我们强制滑到下一个屏幕  
    public void stopMove(){  
          
        Log.v(TAG, "----stopMove ----");  
          
        if(mScroller != null){  
            //如果动画还没结束，我们就按下了结束的按钮，那我们就结束该动画，即马上滑动指定位置  
            if(!mScroller.isFinished()){  
                  
                int scrollCurX= mScroller.getCurrX() ;  
                //判断是否超过下一屏的中间位置，如果达到就抵达下一屏，否则保持在原屏幕  
                // 这样的一个简单公式意思是：假设当前滑屏偏移值即 scrollCurX 加上每个屏幕一半的宽度，除以每个屏幕的宽度就是  
                //  我们目标屏所在位置了。 假如每个屏幕宽度为320dip, 我们滑到了500dip处，很显然我们应该到达第二屏  
                //即(500 + 320/2)/320 = 2  
                int descScreen = ( scrollCurX + getWidth() / 2) / getWidth() ;  
                  
                Log.i(TAG, "-mScroller.is not finished scrollCurX +" + scrollCurX);  
                Log.i(TAG, "-mScroller.is not finished descScreen +" + descScreen);  
                mScroller.abortAnimation();  
  
                //停止了动画，我们马上滑倒目标位置  
                scrollTo(descScreen *getWidth() , 0);  
                curScreen = descScreen ; //纠正目标屏位置  
            }  
            else  
                Log.i(TAG, "----OK mScroller.is  finished ---- ");  
        }  
    }
	
	
}
